package com.example.weight.weidgets;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.weight.R;


public class LoadingDialog extends Dialog implements View.OnLongClickListener {

    private ProgressBar imageView;
    private TextView msgView;

    public static LoadingDialog init(Context context) {
        return DialogHolder.get(context);
    }

    private static class DialogHolder {
        private static LoadingDialog get(Context context) {
            return new LoadingDialog(context);
        }
    }

    private LoadingDialog(Context context) {
        super(context, R.style.load_dialog);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.loading_dialog, null);
        imageView = view.findViewById(R.id.iv_loading);
        msgView = view.findViewById(R.id.tv_msg);
        imageView.setOnLongClickListener(this);
        setContentView(view);
    }

    public void setMsg(String msg){
        msgView.setText(msg);
    }

    /**
     * @param cancelAble 是否可以取消
     */
    public void show(boolean cancelAble) {
        setCancelable(cancelAble);
        show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        //复原默认值：可取消
        setCancelable(true);
    }

    @Override
    public boolean onLongClick(View v) {
        dismiss();
        return true;
    }
}
